#include "dbcolumnsmodel.h"
#include "abstractcontainer.h"
#include "defines.h"
#include "storenums.h"

#include <QMetaObject>
#include <QMetaEnum>
#include <QVariant>
#include <QDebug>

DbColumnsModel::DbColumnsModel(QObject *parent) :
    QAbstractTableModel(parent),
    m_columns(0)
{

}


int DbColumnsModel::rowCount(const QModelIndex &/*parent*/) const
{
    if(m_columns)
        return m_columns->size();
    else
        return 0;
}

int DbColumnsModel::columnCount(const QModelIndex &/*parent*/) const
{
    if( m_columns && m_columns->size()>0 )
    {
        return m_columns->at(0)->dynamicPropertyNames().size();
    }
    else
        return 0;
}

QVariant DbColumnsModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();
    if(role == Qt::TextAlignmentRole)
    {
        return int(Qt::AlignRight | Qt::AlignVCenter);
    }
    else if ( role == Qt::EditRole && m_columns )
    {
        QString s;
        QList<QByteArray> ba = m_columns->at(index.row())->dynamicPropertyNames();
        s = m_columns->at(index.row())->property( ba.at(index.column()) ).toString();
        return s;
    }
    else if ( role == Qt::DisplayRole && m_columns )
    {
        QString s;
        QList<QByteArray> ba = m_columns->at(index.row())->dynamicPropertyNames();
        if( ba.at(index.column()) == XML_COLUMNTYPE )
        {
            s = m_columns->at(index.row())->property( ba.at(index.column()) ).toString();
        }
        else if( ba.at(index.column()) == XML_ISPRIMARYKEY )
        {
            bool val = m_columns->at(index.row())->property( ba.at(index.column()) ).toBool();
            s = val ? "true" : "false";
        }
        else
            s = m_columns->at(index.row())->property( ba.at(index.column()) ).toString();
        return s;
    }
    return QVariant();
}

bool DbColumnsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if( index.isValid() && role == Qt::EditRole && m_columns)
    {
        QList<QByteArray> ba = m_columns->at(index.row())->dynamicPropertyNames();
        if( ba.at(index.column()) == XML_COLUMNLENGTH )
        {
            QString strVal = value.toString();
            if(strVal.length()>0)
                m_columns->at( index.row() )->setProperty( ba.at(index.column() ), strVal.toInt() );
            else
                m_columns->at( index.row() )->setProperty( ba.at(index.column() ), strVal );
        }else
        if( ba.at(index.column()) == XML_ISPRIMARYKEY )
        {
            m_columns->at( index.row() )->setProperty( ba.at(index.column() ), value.toBool() );
            if(value.toBool()==true)
            for(int i = 0; i < rowCount(); i++)
            {
                if( i != index.row() )
                {
                    m_columns->at( i )->setProperty( ba.at(index.column() ), false );
                }
            }
        }
        else
            m_columns->at( index.row() )->setProperty( ba.at(index.column() ), value );

        emit dataChanged(index,index);
        return true;
    }
    return false;
}

QVariant DbColumnsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role == Qt::DisplayRole && orientation == Qt::Horizontal && m_columns && m_columns->size()>0 )
    {
        return m_columns->at(0)->dynamicPropertyNames().at(section);
    }
    else if(role == Qt::DisplayRole && orientation == Qt::Vertical)
    {
        return QVariant(section+1);
    }
    return QVariant();
}

Qt::ItemFlags DbColumnsModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    flags |= Qt::ItemIsEditable;
    return flags;
}

void DbColumnsModel::setColumnsList(AbstractContainer *cols)
{
    m_columns = cols;
    this->reset();
}

bool DbColumnsModel::insertRow(int row, const QModelIndex &parent)
{
    beginInsertRows(parent,row,row);
    AbstractStorage *col = m_columns->appendStorage(XML_COLUMN);
    col->setProperty(XML_COLUMNTYPE,QVariant("VARCHAR"));
    endInsertRows();
    if(m_columns->size()==1)
        this->reset();
    return true;
}

bool DbColumnsModel::removeRow(int row, const QModelIndex &parent)
{
    beginRemoveRows(parent, row, row);
    m_columns->removeAt(row);
    endRemoveRows();
    return true;
}

